package com.example.lotteryticketdev.service.impl;

import com.example.lotteryticketdev.entity.LuckyBall;
import com.example.lotteryticketdev.entity.SpiderResult;
import com.example.lotteryticketdev.result.Result;
import com.example.lotteryticketdev.service.AttemptRequestService;
import com.example.lotteryticketdev.service.LuckyBallService;
import com.example.lotteryticketdev.tool.SpiderTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class AttemptRequestServiceImpl implements AttemptRequestService {

    @Autowired
    private LuckyBallService luckyBallService;

    @Override
    public SpiderResult getPageInfo() {
        return SpiderTool.getJsonResult(1,30);
    }

    @Override
    @Transactional
    public  List<List<LuckyBall>>  getDataByBounds(Integer bounds) {
        SpiderResult pageInfo = this.getPageInfo();
        Integer total = pageInfo.getTotal();
        Integer pageNo = pageInfo.getPageNo();
        Integer pageNum = pageInfo.getPageNum() ;
        Integer pageSize = pageInfo.getPageSize();

        //bounds不能大于总页数
        bounds = bounds > pageNum ? pageNum : bounds;

        List<List<LuckyBall>> luckyBalls = new ArrayList<>();
        for ( ; pageNo <= bounds ; pageNo++) {
            SpiderResult spiderResult = SpiderTool.getJsonResult(pageNo, pageSize);
            //把多次请求的结果放到一个集合中luckyBalls
            luckyBalls.add(spiderResult.getResult());
                try {
                    System.out.println("======>休息10秒再请求");
                    TimeUnit.SECONDS.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
        }
        return luckyBalls;
    }

    @Override
    public Result getAllDataByTwice() {
        SpiderResult pageInfo = this.getPageInfo();
        Integer total = pageInfo.getTotal();
        Integer pageNum = 2 ;
        Integer pageSize =  (total / pageNum) % 2 == 0 ? total / pageNum : total / pageNum + 1;
        Integer pageNo = 1 ;
        return this.request(pageNo,pageNum,pageSize,total);
    }

    private Result request(Integer pageNo,Integer pageNum, Integer pageSize,Integer total){

        for ( ; pageNo <= pageNum ; pageNo++) {
            SpiderResult spiderResult = SpiderTool.getJsonResult(pageNo, pageSize);
            List<LuckyBall> result = spiderResult.getResult();
            boolean b = luckyBallService.saveLuckyBall(result);
            if (b == false){
                return Result.fail("请求失败，在当前页"+pageNo+"时插入报错..");
            }

            try {
                TimeUnit.SECONDS.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("======>休息10秒再请求");
        }
        return Result.ok("请求成功，一共请求"+pageNo+"次，总共获取"+ total +"条数据");
    }
}
